//
//  AXFModelManager.m
//  BEEQUICK
//
//  Created by 李典 on 16/11/22.
//  Copyright © 2016年 SevenGroup. All rights reserved.
//

#import "AXFModelManager.h"

@interface AXFModelManager ()



@end

@implementation AXFModelManager

static id _instance;

- (NSMutableArray *)managerModelList
{
    if (!_managerModelList) {
        _managerModelList = [[NSMutableArray alloc]init];
    }
    return _managerModelList;
}
+(instancetype)shareModelManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    return _instance;
}
- (instancetype)init
{
    if (self = [super init]) {
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(shoppingCart:) name:@"shoppingCart" object:nil];
    }
    return self;
}
- (void)shoppingCart:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    AXFDetailsModel *model = userInfo[@"model"];
    
    if (model.shopCount <= 0) {
        
        // 遍历
        for (int i = 0; i <self.managerModelList.count; i ++) {
            
            AXFDetailsModel *m = self.managerModelList[i];
            
            if ([m.name isEqualToString:model.name]) {
                
                [self.managerModelList removeObject:m];
                
                return;
            }
        }
    }
    
    
    // 遍历数组
    
    for (int i = 0; i < self.managerModelList.count; i ++) {

        AXFDetailsModel *model2 = self.managerModelList[i];
        
        if ([model.name isEqualToString:model2.name]) {
            
            model2.shopCount = model.shopCount;
            
            return;
        }
        
    }

    
    AXFDetailsModel *model1 = [[AXFDetailsModel alloc]init];
    model1.name = model.name;
    model1.price = model.price;
    model1.shopCount = model.shopCount;
    model1.btnSelect = YES;
    
    [self.managerModelList addObject:model1];
    
    
//    AXFDetailsModel *newModel = [[AXFDetailsModel alloc]init];
    
 
//    
//    if ([self.managerModelList containsObject:model]) {
//        
//        //设置model中的选中属性为YES
//        model.btnSelect = YES;
//        
//        self.managerModelList[[self.managerModelList indexOfObject:model]] = model;
//    
//    }else{
//        
//        //设置model中的选中属性为YES
//        model.btnSelect = YES;
//        
//        [self.managerModelList addObject:model];
//    }
//    
//    if (!model.shopCount) {
//        [self.managerModelList removeObject:model];
//    }
    
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"shoppingCart" object:nil];
    
}

//返回你商品的价格
- (float)showTotalPrice
{
    
    __block float price  = 0;
    [self.managerModelList enumerateObjectsUsingBlock:^(AXFDetailsModel* obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if(obj.btnSelect)
        {
            price += obj.shopCount * obj.partner_price;
        }
    }];
    
    return price;
    
    
}


@end
